﻿using System;

namespace JeanFish.Common
{
    /// <summary>
    /// 数组的扩展方法
    /// </summary>
    public static class ArrayHelper
    {
        /// <summary>
        /// 获取指定数组的子数组
        /// </summary>
        /// <typeparam name="T">数组元素类型</typeparam>
        /// <param name="sourceArray">原始数组</param>
        /// <param name="sourceIndex">子数组的初始索引</param>
        /// <param name="length">子数组长度</param>
        /// <returns>子数组</returns>
        public static T[] SubArray<T>(this T[] sourceArray, int sourceIndex, int length)
        {
            T[] temp = new T[length];
            try
            {
                Array.Copy(sourceArray, sourceIndex, temp, 0, length);
            }
            catch (Exception ex)
            {
                ex.Handle(ex.Message);
            }
            return temp;
        }
        /// <summary>
        /// 获取指定数组的子数组
        /// </summary>
        /// <typeparam name="T">数组元素类型</typeparam>
        /// <param name="sourceArray">原始数组</param>
        /// <param name="sourceIndex">子数组的初始索引</param>
        /// <returns>子数组</returns>
        public static T[] SubArray<T>(this T[] sourceArray, int sourceIndex)
        {
            int length = sourceArray.Length - sourceIndex;
            return sourceArray.SubArray(sourceIndex, length);
        }
    }
}
